package com.hna.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class RestUtils {
	public String DoTestRest() {
		String base = "http://10.71.200.109:1174/pm/api";
		String authPath = base + "/rest/authorization/auth/zongcai/764275/1000";

		HttpURLConnection conn;
		try {
			conn = (HttpURLConnection) new URL(authPath).openConnection();
			conn.setRequestProperty("Accept", "application/json");
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");
			int code = conn.getResponseCode();
			
			if("200".equals(code)){
				 InputStream inStream1 = conn.getInputStream();
				 byte[] data = read(inStream1);
			     String jsonResult = new String(data);
				 
	             System.out.println(jsonResult);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		return null;
	}
	
	   public static byte[] read(InputStream inStream) throws Exception {
	        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	        byte[] buffer = new byte[1024];
	        int len = 0;
	        while ((len = inStream.read(buffer)) != -1) {
	            outputStream.write(buffer);
	        }
	        inStream.close();
	        return outputStream.toByteArray();
	    }
}
